<template>
  <div id="login">
    <div
      class="min-w-[400px] p-[20px] py-[30px] pt-[35px] bg-white absolute left-2/4 top-2/4 login-form"
    >
      <div class="text-center text-2xl italic font-bold mb-[20px]">
        通用后台管理系统
      </div>
      <el-form ref="formRef" :model="loginParams" :rules="rules">
        <el-form-item prop="username">
          <el-input
            v-model="loginParams.username"
            placeholder="请输入用户名"
            size="large"
          >
            <template #prefix>
              <i-ep-user-filled />
            </template>
          </el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="loginParams.password"
            type="password"
            placeholder="请输入密码"
            size="large"
            show-password
          >
            <template #prefix>
              <i-ep-key />
            </template>
          </el-input>
        </el-form-item>
        <el-form-item class="w-full" prop="captcha">
          <div class="flex w-full gap-3">
            <div class="grow">
              <el-input
                v-model="loginParams.captcha"
                placeholder="请输入验证码"
                size="large"
              >
                <template #prefix>
                  <i-ep-view />
                </template>
              </el-input>
            </div>
            <div class="flex-none cursor-pointer">
              <img
                class="max-w-full max-h-[38px] w-full"
                :src="captchaImg"
                @click="getCaptche()"
              />
            </div>
          </div>
        </el-form-item>
        <el-form-item class="flex mt-[30px]">
          <el-button class="flex-1" type="primary" size="large" @click="Login">
            登录
          </el-button>
          <el-button
            class="flex-1"
            type="info"
            size="large"
            @click="resetForm(formRef)"
          >
            重置
          </el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { onMounted, reactive, ref } from 'vue'
import { captcha, login } from '@/api/login/login'
import { LoginParams } from '@/api/login/model/loginModel'
import { ElMessage, FormInstance, FormRules } from 'element-plus'
import { LoginStore } from '@/store/login'
import router from '@/router'
import { useForm } from '@/hooks'

const formRef = ref<FormInstance>(null!)
const loginStore = LoginStore()
const { resetForm, validate } = useForm()

var captchaImg = ref('')
var loginParams = reactive<LoginParams>({
  username: '',
  password: '',
  idKey: '',
  captcha: '',
})

const rules = reactive<FormRules<LoginParams>>({
  username: [
    {
      required: true,
      message: '请输入用户名',
      trigger: 'blur',
    },
  ],
  password: [
    {
      required: true,
      message: '请输入密码',
      trigger: 'blur',
    },
  ],
  idKey: [
    {
      required: true,
      message: '缺少必要信息（key）',
      trigger: 'blur',
    },
  ],
  captcha: [
    {
      required: true,
      message: '请输入验证码',
      trigger: 'blur',
    },
  ],
})

//获取验证码
async function getCaptche() {
  const { code, data, message } = await captcha()
  if (code !== 200) {
    ElMessage.error(message)
    return
  }
  captchaImg.value = data?.captcha || ''
  loginParams.idKey = data?.id || ''
}

const Login = async () => {
  await validate(formRef.value)
  //发送请求
  const { code, data, message } = await login(loginParams)
  if (code != 200) {
    ElMessage.error(message)
    return
  }
  ElMessage.success('登录成功')
  loginStore.setValue('token', 'Bearer ' + (data?.token || ''))
  loginStore.setValue('leftMenuList', data?.leftMenuList || [])
  loginStore.setValue('permissionList', data?.permissionList || [])
  loginStore.setValue('sysAdmin', data?.sysAdmin || null)
  router.replace({ path: '/home' })
}

onMounted(() => {
  getCaptche()
})
</script>

<style scoped>
#login {
  width: 100%;
  height: 100%;
  background-image: url('@/assets/images/login-background.jpg');
  background-repeat: no-repeat;
  background-size: cover;
  position: absolute;
  top: 0;
  left: 0;
}
.login-form {
  transform: translate(-50%, -50%);
}
</style>
